<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021 http://115cms.com.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 作者@梦雨 <50361804@qq.com>
// +----------------------------------------------------------------------
class Model extends Controller
{
    protected function _initialize()
    { 
        hook('phpmodel');
    }
    //模型首页
    public function index()
    {
        hook('phpindexindex');
        $c = Db::name('model')->where(array('name' => input('model')))->find();
        $this->assign('c', $c);
        if (guoji($c['name'].'_model')) {
            $muban='./app/template/'.muban().'/model/'.dingyi(guoji($c['name'].'_model'),'index').'.html';
        } else {
            $muban='./app/template/'.muban().'/model/index_'.input('model').'.html';
            if(!is_file($muban)){
    	        $muban='./app/template/'.muban().'/model/index'.'.html';
            }
        }
        if(!is_file($muban)){
	        exit('模板文件不存在请创建模板 '.$muban);
        }
        $this->assign('hover', input('model'));
        $this->assign('m', input('model'));
        return view($muban);
    }
    //分类
    public function cate()
    {
        hook('phpmodelcate');
        if (input('alias')) {
            $c = Db::name('cate_m')->where(array('alias' => input('alias')))->find();
            $this->assign('c', $c);
        } 
        // 始终获取顶级的id
        if ($c['tid']==0) {
            $id=$c['id'];
            $name=$c['name'];
        } else {
            $id=$c['tid'];
            $name=fun_sc_p('cate_m', array('id' => $c['tid']), 'name');
        }
        $this->assign('id', $id);
        $this->assign('name', $name);
        if (guoji($c['alias'].'_'.$c['m'])) {
            $muban='./app/template/'.muban().'/model/'.dingyi(guoji($c['alias'].'_'.$c['m']),'cate').'.html';
        } else {
            $muban='./app/template/'.muban().'/model/cate_'.$c['m'].'.html';
            if(!is_file($muban)){
    	        $muban='./app/template/'.muban().'/model/cate'.'.html';
            }
        }
        if(!is_file($muban)){
	        exit('模板文件不存在请创建模板 '.$muban);
        }
        $this->assign('hover', $c['m']);
        $this->assign('m', $c['m']);
        return view($muban);
    }
    // 详情页面
    public function html()
    {
        hook('phpindexhtml');
        $t = Db::name(input('model'))->where(array('id' => input('id'), 'open' => 1))->find();
        $this->assign('t', $t);
        $cate = Db::name('cate_m')->where(array('id' => $t['tid']))->find();
        $this->assign('cate', $cate);
        $fufei=0;
        // 判断是否需要付费
        if($t['jifen']!=0){
            $fufei=1;
            //判断是否付费
            if(fun_st('point_note',array('title'=>'付费阅读'.input('model'),'tid'=>$t['id'],'uid'=>user('userid')))!=0){
                $fufei=0;
            }
        }
        $this->assign('fufei', $fufei);
        if (empty($t['id'])) {
            return $this->error('页面不存在','/');
        }
        Db::name(input('model'))->where('id', input('id'))->setInc('view', 1);
        
        if (guoji($cate['alias'].'html_'.$cate['m'])) {
            $muban='./app/template/'.muban().'/model/'.dingyi(guoji($cate['alias'].'html_'.$cate['m']),'html').'.html';
        } else {
            $muban='./app/template/'.muban().'/model/html_'.$cate['m'].'.html';
            if(!is_file($muban)){
    	        $muban='./app/template/'.muban().'/model/html'.'.html';
            }
        }
        if(!is_file($muban)){
	        exit('模板文件不存在请创建模板 '.$muban);
        }
        $this->assign('hover', $cate['m']);
        $this->assign('m', $cate['m']);
        return view($muban);
    }
    // 搜索
    public function soso()
    {
        hook('phpmodelsoso');
        if (guoji(input('m').'_soso')) {
            $muban='./app/template/'.muban().'/model/'.dingyi(guoji(input('m').'_soso'),'soso').'.html';
        } else {
            $muban='./app/template/'.muban().'/model/soso_'.input('m').'.html';
            if(!is_file($muban)){
    	        $muban='./app/template/'.muban().'/model/soso'.'.html';
            }
        }
        if(!is_file($muban)){
	        exit('模板文件不存在请创建模板 '.$muban);
        }
        $this->assign('hover', input('m'));
        $this->assign('m', input('m'));
        return view($muban);
    }
}